package com.originskyseed.platform.user.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.BusinessRelateChannel;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IBusinessRelateChannelService;
import com.originskyseed.platform.user.vo.request.BusinessRelateChannelRequestVO;

/**
 * 业务员关联渠道信息表Controller类（OSI_BUSINESS_RELATE_CHANNEL）
 *
 * @author WuZhuoXi
 */
@Controller
@RequestMapping("/businessRelateChannel")
@BaseLocaleResource(localeFileName = "i18n_user")
public class BusinessRelateChannelController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(BusinessRelateChannelController.class);

	@DubboReference
	private IBusinessRelateChannelService businessRelateChannelService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/businessRelateChannelList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<BusinessRelateChannel>> businessRelateChannelList(HttpServletRequest request) {
		BaseResponseBody<BasePage<BusinessRelateChannel>> result = null;
		try {
			// 请求对象
			BusinessRelateChannelRequestVO requestVO = this.getParameterBean(BusinessRelateChannelRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			requestVO.setCreatorId(userInfo.getId());
			// 分页查询
			BasePage<BusinessRelateChannel> page = new BasePage<BusinessRelateChannel>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.businessRelateChannelService.selectListByPage(requestVO.toMap(), page, userInfo);
			result = new BaseResponseBody<BasePage<BusinessRelateChannel>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<BusinessRelateChannel>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/businessRelateChannelSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<BusinessRelateChannel> businessRelateChannelSave(HttpServletRequest request) {
		BaseResponseBody<BusinessRelateChannel> result = null;
		try {
			// 请求对象
			BusinessRelateChannel businessRelateChannel = this.getParameterBean(BusinessRelateChannel.class);

			if (StringUtils.isNotBlank(businessRelateChannel.getId())) {
				this.businessRelateChannelService.updateByPrimaryKeySelective(businessRelateChannel);
			} else {
				// 校验是否已经绑定
				BusinessRelateChannel channel = this.businessRelateChannelService.selectIsChannelByBusinessId(businessRelateChannel.getBusinessId());
				if (channel != null) {
					return new BaseResponseBody<BusinessRelateChannel>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.USER_DATA_ISNOTNULL.getMsg()), null);
				}
				UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
				businessRelateChannel.setCreator(userInfo.getName());
				businessRelateChannel.setCreatorId(userInfo.getId());
				businessRelateChannel.setIsDel(2);
				this.businessRelateChannelService.insertSelective(businessRelateChannel);
			}
			result = new BaseResponseBody<BusinessRelateChannel>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), businessRelateChannel);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BusinessRelateChannel>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/businessRelateChannelDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> businessRelateChannelDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			this.businessRelateChannelService.updateDeleteByIds(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
